package com.example.ec.controller;

import com.example.ec.model.Goods;
import com.example.ec.model.User;
import com.example.ec.service.GoodsService;
import com.example.ec.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController extends BaseController{
    private UserService userService;
    private GoodsService goodsService;


    public UserController(UserService userService, GoodsService goodsService) {
        this.userService = userService;
        this.goodsService = goodsService;
    }

    @RequestMapping("signUp")
    private String signUp(User user){
        if(userService.query("queryByUsername",user.getUsername())==null){
            userService.create(user);
            return "redirect:/index.jsp";}
        else {
            session.setAttribute("error","Username is exist...");
            return "redirect:/signUp.jsp";

        }

    }
    @RequestMapping("signIn")
    private String signIn(User user){
        if((user= (User) userService.query("queryByUsernameAndPassword",user))==null){
            session.setAttribute("error","Username or Password is error");
            return "redirect:/index.jsp";
        }else {
            if(user.getRole()==0){
                session.setAttribute("goods",goodsService.queryAll());
                return "redirect:/user/user.jsp";
            }else {
                session.setAttribute("user",user);
                session.setAttribute("goods",goodsService.queryAll());
                return "redirect:/admin/admin.jsp";
            }
        }


    }

}
